package com.mj.mafeed.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mj.mafeed.R;
import com.mj.mafeed.activity.FavoriteListActivity;
import com.mj.mafeed.bean.ProductJSON;
import com.mj.mafeed.config.Constants;
import com.mj.mafeed.service.FavoriteService;
import com.mj.mafeed.util.NetUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
/**
 * 关注产品列表
 * @author zhaominglei
 * @date 2014-12-16
 * 
 */
public class FavoriteAdapter extends BaseAdapter {
	private List<ProductJSON> products;
	private Context context;
	private FavoriteListActivity favoriteListActivity;
	@SuppressWarnings("unused")
	private ProgressBar progressBar;
	private ImageLoader imageLoader = null;
	private DisplayImageOptions options = null;
	private Long productId;
	private String referer; //来自那个链接

	@SuppressWarnings("deprecation")
	public FavoriteAdapter(Context context, List<ProductJSON> products) {
		super();
		this.context = context;
		favoriteListActivity = (FavoriteListActivity)context;
		this.products = products;
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(context));

		options = new DisplayImageOptions.Builder()
				.displayer(new RoundedBitmapDisplayer(0xff000000, 10))
				.cacheInMemory().cacheOnDisc().build();
	}

	public List<ProductJSON> getProducts() {
		return products;
	}

	public void setProducts(List<ProductJSON> products) {
		this.products.addAll(products);
	}
	@Override
	public int getCount() {
		return products.size();
	}

	@Override
	public Object getItem(int position) {
		return products.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout relativeLayout;
		if (convertView != null) {
			relativeLayout = (RelativeLayout) convertView;
		} else {
			relativeLayout = (RelativeLayout) View.inflate(context,
					R.layout.favorite_item, null);
		}
		progressBar = (ProgressBar) relativeLayout.findViewById(R.id.favorite_item_progress);
		ImageView imageView = (ImageView) relativeLayout
				.findViewById(R.id.favorite_item_image);

		imageLoader.displayImage(Constants.SERVER_DOWNLOAD_URL+products.get(position).getImageUrl(),
				imageView, options);
		((TextView) relativeLayout
				.findViewById(R.id.favorite_item_template_name))
				.setText(products.get(position).getName());
		((TextView) relativeLayout
				.findViewById(R.id.favorite_item_template_price))
				.setText(String.valueOf(products.get(position).getPrice()));
		((TextView) relativeLayout
				.findViewById(R.id.favorite_item_template_id))
				.setText(String.valueOf(products.get(position).getProductId()));
		productId = products.get(position).getProductId();
		Button deleteBtn = (Button) relativeLayout.findViewById(R.id.favorite_delete);
		deleteBtn.setTag(productId);
		deleteBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Long descProductId = (Long)v.getTag();
				if (descProductId.equals(productId)) {
					if (NetUtils.isConnected(favoriteListActivity)) {
						new FavoriteDeleteAsyncTask().execute("");
					} else {
						Toast.makeText(favoriteListActivity, R.string.net_error, Toast.LENGTH_SHORT).show();
						return;
					}
				}
			}
		});
		
		return relativeLayout;
	}

	public class FavoriteDeleteAsyncTask extends AsyncTask<String, String, Boolean> {

		@Override
		protected Boolean doInBackground(String... params) {
			return new FavoriteService().deletFavorite(favoriteListActivity, referer, productId);
		}

		@Override
		protected void onPostExecute(Boolean result) {
			if (result) {
				Toast.makeText(favoriteListActivity, R.string.cancel_success, Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(favoriteListActivity, FavoriteListActivity.class);
				intent.putExtra("referer", "FavoriteAdapter");
				favoriteListActivity.startActivity(intent);
				favoriteListActivity.finish();
				return;
			} else {
				Toast.makeText(favoriteListActivity, R.string.cancel_error, Toast.LENGTH_SHORT).show();
				return;
			}
		}
	}
}
